package com.itdfq.images.controller;

import com.github.pagehelper.PageInfo;
import com.itdfq.images.domain.Images;
import com.itdfq.images.service.ImagesService;
import com.itdfq.images.utils.MinioxUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;

import java.util.*;

/**
 * @Author GocChin
 * @Date 2021/4/14 18:32
 * @Blog: itdfq.com
 * @QQ: 909256107
 *
 * @CrossOrigin注解   maxAge：飞行前响应的缓存持续时间的最大年龄（以秒为单位）。   origins  ： 允许可访问的域列表
 */
@RestController
@RequestMapping("/images")
@CrossOrigin
public class ImagesController {
    @Autowired
    private ImagesService imagesService;

    @Autowired
    MinioxUtils minioUtils;


    private String path;
    public String getpath(){
        return path;
    }
    public void setpath(String path){
        this.path=path;
    }

     Map<String,Object> map = new HashMap<>();

     @RequestMapping("/findByPage")
     public Map<String,Object> findByPage(@RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "10") int limit){
         map.clear();
         try {
             List<Images> byPage = imagesService.findByPage();
             PageInfo<Images> pageInfo = new PageInfo<>(byPage);
             map.put("count",pageInfo.getTotal());
             map.put("data",pageInfo.getList());
             map.put("msg",1);
             map.put("code",0);
         } catch (Exception e) {
             e.printStackTrace();
             map.put("msg",e.getMessage());
         }
         return  map;
     }

    @RequestMapping("/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile upload) throws Exception {
        map.clear();
        try {

            String filename = upload.getOriginalFilename();
            minioUtils.uploadToMinio(upload.getInputStream(), filename);
            System.out.println(upload.getContentType());
            boolean b = minioUtils.putObject(minioUtils.getBucketName(), filename, upload.getInputStream(), upload.getContentType());
            if (b){
                String test = minioUtils.getObjectUrl(minioUtils.getBucketName(), filename);  //下载链接
                System.out.println("下载链接为："+test);
                map.put("msg",1);
                setpath(test);
            }else {
                map.put("msg","上传出错");
            }
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg",e.getMessage());
        }

        return map;
    }
     @RequestMapping("/insert")
     public Map<String,Object> insert(@RequestBody Images images)  {
         images.setPath(getpath());
         map.clear();
         try {
             imagesService.insert(images);
             map.put("msg",1);
         } catch (Exception e) {
             e.printStackTrace();
             map.put("msg",e.getMessage());
         }
         return map;
     }



    @RequestMapping("/deleteSelect")
    public Map<String,Object> deleteSelect(String custId){
        System.out.println(custId);
        map.clear();
        try {
            List<String> list = new ArrayList<>();
            String[] strs = custId.split(",");
            for (String str : strs) {
                list.add(str);
            }
            imagesService.deleteSelect(list);
            map.put("msg",1);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg",e.getMessage());
        }
        return map;
    }


}
